package com.shizhanzhe.gxb.circle;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.base.TabAdapter;
import com.shizhanzhe.gxb.circle.fragment.CircleFormFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by YJ on 2019/1/8.
 */
public class CircleFragment extends Fragment {

    public static String[] tabTitle = {"商品推荐", "营销素材", "新手必发"};
    public static String[] ids = {"1", "2", "3"};
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_circle, container, false);
        unbinder = ButterKnife.bind(this, view);
        initImmersionBar();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        List<String> names = new ArrayList<>();
        for (int i=0;i<tabTitle.length;i++){
            names.add(tabTitle[i]);
        }
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            fragments.add(CircleFormFragment.newInstance(ids[i]));
        }
        TabAdapter adapter = new TabAdapter(getChildFragmentManager(), fragments, names);
        //给ViewPager设置适配器
        viewpager.setAdapter(adapter);
        //将TabLayout和ViewPager关联起来。
        tablayout.setupWithViewPager(viewpager);
        //设置可以滑动
        tablayout.setTabMode(TabLayout.MODE_FIXED);
    }
    protected ImmersionBar mImmersionBar;
    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
        mImmersionBar.statusBarView(R.id.bar_0_view)
                .init();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && mImmersionBar != null)
            ImmersionBar.with(this)
                    .statusBarView(R.id.bar_0_view)
                    .init();
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
